<?php 
namespace Api\Controller;
use Think\Controller;
class ApprovalController extends BaseController{
	public function apprlist(){
		//获取客户端发来的用户名
		$user=I('post.user');
		$param=I('post.param');
		$login=M('login');
		$Model=M('dksx');
		//获得用户信息
		$data=$login->where(['CzyName'=>$user])->find();
		
		//根据用户权限取得数据
		if ($data['qxshowall']==1) {//数据全部显示
			if ($param=='businessListManager'||$param=='') {
				//业务审核数据列表
				$Data=$Model->field('id,Pzh,dksqsxmoney,uname,lrdate,Ywsplx')->where(['YwSplx'=>'未审'])->order('lrdate desc')->select();
			}
			if ($param=='risk_check') {
				//风控审核数据列表
				$Data=$Model->where(['YwSplx'=>'通过','FkSplx'=>'未审'])->field('id,Pzh,dksqsxmoney,uname,lrdate,FkSplx,YwSpDate')->order('YwSpDate desc')->select();
			}
			if ($param=='riskManager_check') {
				//风控负责人审核数据列表
				$Data=$Model->where(['FkSplx'=>'通过','FkFSplx'=>'未审'])->field('id,Pzh,dksqsxmoney,uname,lrdate,FkFSplx,FkSpDate')->order('FkSpDate desc')->select();
			}
			if ($param=='topManager_check') {
				//总经理审核数据列表
				$Data=$Model->where(['FkFSplx'=>'通过','JlSplx'=>'未审'])->field('id,Pzh,dksqsxmoney,uname,lrdate,jlsplx,FkFspdate')->order('FkFspdate desc')->select();
			}
			

			$this->ajaxReturn(['code'=>101,'msg'=>'全部查询成功','data'=>$Data]);

		}else{//用户不全部显示，只显示用户自己录入的数据




				if ($param=='businessListManager'||$param=='') {
					//业务审核数据列表
					$Data=$Model->field('id,Pzh,dksqsxmoney,uname,lrdate,Ywsplx')->where(['Lrczy'=>$user,'YwSplx'=>'未审'])->order('lrdate desc')->select();
				}
				if ($param=='risk_check') {
					//风控审核数据列表
					$Data=$Model->where(['YwSplx'=>'通过','Lrczy'=>$user,'FkSplx'=>'未审'])->field('id,Pzh,dksqsxmoney,uname,lrdate,FkSplx,YwSpDate')->order('YwSpDate desc')->select();
				}
				if ($param=='riskManager_check') {
					//风控负责人审核数据列表
					$Data=$Model->where(['FkSplx'=>'通过','Lrczy'=>$user,'FkFSplx'=>'未审'])->field('id,Pzh,dksqsxmoney,uname,lrdate,FkFSplx,FkSpDate')->order('FkSpDate desc')->select();
				}
				if ($param=='topManager_check') {
					//总经理审核数据列表
					$Data=$Model->where(['FkFSplx'=>'通过','Lrczy'=>$user,'JlSplx'=>'未审'])->field('id,Pzh,dksqsxmoney,uname,lrdate,jlsplx,FkFspdate')->order('FkFspdate desc')->select();
				}
				
			
			
				$this->ajaxReturn(['code'=>101,'msg'=>'个人权限查询成功','data'=>$Data]);

		}


	}

	public function apprdetail(){//审核详情页数据
		$id=I('post.id');
		$Model=M('dksx');
		$data=$Model->where(['id'=>$id])->find();
		$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);



	}
	public function dkappr(){
		//接收的参数
		$type=I('post.type');
		$id=I('post.id');
		$text=I('post.data');
		$bm=I('post.param');//数据库审批的字段
		$user=I('post.user');//用户名
		// 实例化对象查询用户信息
		$login=M('login');
		$data=$login->where(['CzyName'=>$user])->find();
		
		$Model=M('dksx');
		$SxData=$Model->where(['id'=>$id])->find();
		
		if (!$SxData) {
			$this->ajaxReturn(['code'=>102,'msg'=>'订单不存在']);die;
		}
		//如果是通过审核的操作
		if ($type==1) {
			// 根据不同的审批来判断
				if ($bm=='businessListManager'||$bm=='') {//业务审批
						if ($data['qxywfzrsp']==1) {
							$Mdata['YwSplx'] = '通过';
							$Mdata['YwSpyj']=$text;
							$Mdata['YwSpr']=$user;
							
							$Mdata['YwSpDate']=date('Y-m-d 00:00:00.000');

							if ($SxData['ywsplx']=='通过') {
								$this->ajaxReturn(['code'=>102,'msg'=>'已经审核过了']);die;
							}else{
								$Model->where(['id'=>$id])->save($Mdata); // 根据条件更新记录
							if ($Model) {
								//操作写入日志
								$admin=M('log');
								$row = array(
										
										'czLr' => $data['czyname'].'通过了业务审核,授信单号：'.$SxData['pzh'].',客户：'.$SxData['uname'],
										'CzName' => $data['czyname'],
										'czdate' => date('Y-m-d H:i:s',time()),
										'gs'=>'jsapp',
										'yyyy'=>'fox'
									);
								$admin->add($row);
								$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);
								}
							}
							
							}else{

								$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);

						}
				}


				if($bm=='risk_check'){//风控审批
						if ($data['qxfksp']==1) {
							$Mdata['FkSplx'] = '通过';
							$Mdata['FkSpyj']=$text;
							$Mdata['FkSpr']=$user;
							
							$Mdata['FkSpDate']=date('Y-m-d 00:00:00.000');
							if ($SxData['fksplx']=='通过') {
								$this->ajaxReturn(['code'=>102,'msg'=>'已经审核过了']);die;
							}
							$Model->where(['id'=>$id])->save($Mdata); // 根据条件更新记录
							if ($Model) {
								//操作写入日志
								$admin=M('log');
								$row = array(
										
										'czLr' => $data['czyname'].'通过了风控审核,授信单号：'.$SxData['pzh'].',客户：'.$SxData['uname'],
										'CzName' => $data['czyname'],
										'czdate' => date('Y-m-d H:i:s',time()),
										'gs'=>'jsapp',
										'yyyy'=>'fox'
									);
								$admin->add($row);
								$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);
								}
							}else{

								$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);

						}


				}



				if($bm=='riskManager_check'){//风控负责人审批
						if ($data['qxfkfzrsp']==1) {
							$Mdata['FkFSplx'] = '通过';
							$Mdata['FkFSpyj']=$text;
							$Mdata['FkFSpr']=$user;
							
							$Mdata['FkFSpDate']=date('Y-m-d 00:00:00.000');
							if ($SxData['fkfsplx']=='通过') {
								$this->ajaxReturn(['code'=>102,'msg'=>'已经审核过了']);die;
							}
							$Model->where(['id'=>$id])->save($Mdata); // 根据条件更新记录
							if ($Model) {
								//操作写入日志
								$admin=M('log');
								$row = array(
										
										'czLr' => $data['czyname'].'通过了风控负责人审核,授信单号：'.$SxData['pzh'].',客户：'.$SxData['uname'],
										'CzName' => $data['czyname'],
										'czdate' => date('Y-m-d H:i:s',time()),
										'gs'=>'jsapp',
										'yyyy'=>'fox'
									);
								$admin->add($row);
								$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);
								}
							}else{

								$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);

						}


				}


				if($bm=='topManager_check'){//总经理审批
						if ($data['qxjlsp']==1) {
							$Mdata['JlSplx'] = '通过';
							$Mdata['JlSpyj']=$text;
							$Mdata['JlSpr']=$user;
							
							$Mdata['JlSpDate']=date('Y-m-d 00:00:00.000');
							if ($SxData['jlsplx']=='通过') {
								$this->ajaxReturn(['code'=>102,'msg'=>'已经审核过了']);die;
							}
							$Model->where(['id'=>$id])->save($Mdata); // 根据条件更新记录
							if ($Model) {
								//操作写入日志
								$admin=M('log');
								$row = array(
										
										'czLr' => $data['czyname'].'通过了总经理审核,授信单号：'.$SxData['pzh'].',客户：'.$SxData['uname'],
										'CzName' => $data['czyname'],
										'czdate' => date('Y-m-d H:i:s',time()),
										'gs'=>'jsapp',
										'yyyy'=>'fox'
									);
								$admin->add($row);
								$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);
								}
							}else{

								$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);

						}


				}


		}else{//如果是拒绝审核的操作
				if ($bm=='businessListManager'||$bm=='') {
						if ($data['qxywfzrsp']==1) {
							$Mdata['YwSplx'] = '拒绝';
							$Mdata['YwSpyj']=$text;
							$Mdata['YwSpr']=$user;
							
							$Mdata['YwSpDate']=date('Y-m-d 00:00:00.000');
							if ($SxData['ywsplx']=='拒绝') {
								$this->ajaxReturn(['code'=>102,'msg'=>'已经审核过了']);die;
							}
							$Model->where(['id'=>$id])->save($Mdata); // 根据条件更新记录
							if ($Model) {
								//操作写入日志
								$admin=M('log');
								$row = array(
										
										'czLr' => $data['czyname'].'拒绝了业务审核,授信单号：'.$SxData['pzh'].',客户：'.$SxData['uname'],
										'CzName' => $data['czyname'],
										'czdate' => date('Y-m-d H:i:s',time()),
										'gs'=>'jsapp',
										'yyyy'=>'fox'
									);
								$admin->add($row);
								$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);
								}
							}else{

								$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);

						}
				}

			
				if($bm=='risk_check'){
						if ($data['qxfksp']==1) {
							$Mdata['FkSplx'] = '拒绝';
							$Mdata['FkSpyj']=$text;
							$Mdata['FkSpr']=$user;
							
							$Mdata['FkSpDate']=date('Y-m-d 00:00:00.000');
							if ($SxData['fksplx']=='拒绝') {
								$this->ajaxReturn(['code'=>102,'msg'=>'已经审核过了']);die;
							}
							$Model->where(['id'=>$id])->save($Mdata); // 根据条件更新记录
							if ($Model) {
								//操作写入日志
								$admin=M('log');
								$row = array(
										
										'czLr' => $data['czyname'].'拒绝了风控审核,授信单号：'.$SxData['pzh'].',客户：'.$SxData['uname'],
										'CzName' => $data['czyname'],
										'czdate' => date('Y-m-d H:i:s',time()),
										'gs'=>'jsapp',
										'yyyy'=>'fox'
									);
								$admin->add($row);
								$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);
								}
							}else{

								$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);

						}


				}



				if($bm=='riskManager_check'){
						if ($data['qxfkfzrsp']==1) {
							$Mdata['FkFSplx'] = '拒绝';
							$Mdata['FkFSpyj']=$text;
							$Mdata['FkFSpr']=$user;
							
							$Mdata['FkFSpDate']=date('Y-m-d 00:00:00.000');
							if ($SxData['fkfsplx']=='拒绝') {
								$this->ajaxReturn(['code'=>102,'msg'=>'已经审核过了']);die;
							}
							$Model->where(['id'=>$id])->save($Mdata); // 根据条件更新记录
							if ($Model) {
								//操作写入日志
								$admin=M('log');
								$row = array(
										
										'czLr' => $data['czyname'].'拒绝了风控负责人审核,授信单号：'.$SxData['pzh'].',客户：'.$SxData['uname'],
										'CzName' => $data['czyname'],
										'czdate' => date('Y-m-d H:i:s',time()),
										'gs'=>'jsapp',
										'yyyy'=>'fox'
									);
								$admin->add($row);
								$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);
								}
							}else{

								$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);

						}


				}


				if($bm=='topManager_check'){
						if ($data['qxjlsp']==1) {
							$Mdata['JlSplx'] = '拒绝';
							$Mdata['JlSpyj']=$text;
							$Mdata['JlSpr']=$user;
							
							$Mdata['JlSpDate']=date('Y-m-d 00:00:00.000');
							if ($SxData['jlsplx']=='拒绝') {
								$this->ajaxReturn(['code'=>102,'msg'=>'已经审核过了']);die;
							}
							$Model->where(['id'=>$id])->save($Mdata); // 根据条件更新记录
							if ($Model) {
								//操作写入日志
								$admin=M('log');
								$row = array(
										
										'czLr' => $data['czyname'].'拒绝了总经理审核,授信单号：'.$SxData['pzh'].',客户：'.$SxData['uname'],
										'CzName' => $data['czyname'],
										'czdate' => date('Y-m-d H:i:s',time()),
										'gs'=>'jsapp',
										'yyyy'=>'fox'
									);
								$admin->add($row);
								$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);
								}
							}else{

								$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);

						}


				}









		}

	}






}









// if ($data['qxywfzrsp']==1) {
// 				$YwData=$Model->field('Pzh,dksqsxmoney,uname,lrdate,Ywsplx,ywspdate')->order('ywspdate desc')->select();
				
// 			}
// 			if ($data['qxfksp']==1) {
// 				$FkData=$Model->where(['YwSplx'=>'通过'])->field('id,Pzh,dksqsxmoney,uname,lrdate,FkSplx,FkSpDate')->order('FkSpDate desc')->select();
				
// 			}
// 			if ($data['qxfkfzrsp']==1) {
// 				$FkfData=$Model->where(['FkSplx'=>'通过'])->field('id,Pzh,dksqsxmoney,uname,lrdate,FkFSplx,FkFspdate')->order('FkFspdate desc')->select();
				
// 			}
// 			if ($data['qxjlsp']==1) {
// 				$JlData=$Model->where(['FkFSplx'=>'通过'])->field('id,Pzh,dksqsxmoney,uname,lrdate,jlsplx,jlspdate')->order('jlspdate desc')->select();
			
// 			}




			// $Mdata[$bm] = '通过';
			
			// $Model->where(['id'=>$id])->save($Mdata); // 根据条件更新记录

			// if ($Model) {
			// 	//操作写入日志
			// 	$admin=M('log');
			// 	$row = array(
						
			// 			'czLr' => $data['czyname'].'通过了审核',
			// 			'CzName' => $data['czyname'],
			// 			'czdate' => date('Y-m-d H:i:s',time()),
			// 			'gs'=>'jsapp',
			// 			'yyyy'=>'fox'
			// 		);
			// 	$admin->add($row);


			// 	$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);
			// }else{
			// 	$this->ajaxReturn(['code'=>101,'msg'=>'操作失败']);
			// }



 ?>